<script lang="ts" setup>
import type { UploadProps, UploadRequestOptions } from 'element-plus'

defineOptions({
  inheritAttrs: false,
})
const props = withDefaults(
  defineProps<{
    onSuccess?: UploadProps['onSuccess']
    publicAccess?: boolean // 是否是私有OSS
  }>(),
  {
    publicAccess: true,
  },
)
async function httpRequest(options: UploadRequestOptions) {
  return await service.common.upload({
    onProgress: options.onProgress,
    file: options.file,
    filename: options.filename,
    publicAccess: props.publicAccess,
  })
}
const onSuccess: UploadProps['onSuccess'] = (response, uploadFile, uploadFiles) => {
  const { url, objectName } = response
  if (!props.publicAccess) {
    uploadFile.url = oss.get(objectName)
  } else {
    uploadFile.url = url
  }
  if (props.onSuccess) {
    props.onSuccess(response, uploadFile, uploadFiles)
  }
}
</script>

<template>
  <el-upload v-bind="$attrs" :http-request="httpRequest" :on-success="onSuccess">
    <template v-if="$slots.default" #default>
      <slot name="default" />
    </template>
    <template v-if="$slots.trigger" #trigger>
      <slot name="trigger" />
    </template>
    <template v-if="$slots.file" #file="{ file }">
      <slot name="file" :file="file" />
    </template>
    <template v-if="$slots.tip" #tip>
      <slot name="tip" />
    </template>
  </el-upload>
</template>
